/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.server;

import com.jfinal.kit.StrKit;
import com.lambkit.Lambkit;
import com.lambkit.common.util.ScanJarStringSource;
import com.lambkit.db.LambkitDataSource;
import com.lambkit.module.LambkitModule;

import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.UpmsConfig;
import com.lambkit.module.upms.model.*;
import com.lambkit.module.upms.record.*;

import com.lambkit.plugin.rpc.RpcConfig;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2024-05-07
 * @version 1.0
 * @since 1.0
 */
public class UpmsModule extends LambkitModule  {

	@Override
	public void configMapping(LambkitDataSource dataSource) {
		if(StrKit.isBlank(getConfig().getDbconfig())) {
			mapping(dataSource);
		}
	}
	
	@Override
	public void configMapping(String name, LambkitDataSource dataSource) {
		super.configMapping(name, dataSource);
		if(StrKit.notBlank(name) && name.equals(getConfig().getDbconfig())) {
			mapping(dataSource);
		}
	}
	
	public void mapping(LambkitDataSource dataSource) {
		dataSource.addSqlTemplate(new ScanJarStringSource("upms.sql"));
		UpmsManager.me().setUseable(true);
		dataSource.addMapping("upms_auth_code", "id", UpmsAuthCodeModel.class, UpmsAuthCode.class);
		dataSource.addMapping("upms_credit", "id", UpmsCreditModel.class, UpmsCredit.class);
		dataSource.addMapping("upms_favorites", "id", UpmsFavoritesModel.class, UpmsFavorites.class);
		dataSource.addMapping("upms_log", "log_id", UpmsLogModel.class, UpmsLog.class);
		dataSource.addMapping("upms_organization", "organization_id", UpmsOrganizationModel.class, UpmsOrganization.class);
		dataSource.addMapping("upms_permission", "permission_id", UpmsPermissionModel.class, UpmsPermission.class);
		dataSource.addMapping("upms_role", "role_id", UpmsRoleModel.class, UpmsRole.class);
		dataSource.addMapping("upms_role_permission", "role_permission_id", UpmsRolePermissionModel.class, UpmsRolePermission.class);
		dataSource.addMapping("upms_role_rule", "rule_role_id", UpmsRoleRuleModel.class, UpmsRoleRule.class);
		dataSource.addMapping("upms_rule", "id", UpmsRuleModel.class, UpmsRule.class);
		dataSource.addMapping("upms_system", "system_id", UpmsSystemModel.class, UpmsSystem.class);
		dataSource.addMapping("upms_tag", "id", UpmsTagModel.class, UpmsTag.class);
		dataSource.addMapping("upms_user", "user_id", UpmsUserModel.class, UpmsUser.class);
		dataSource.addMapping("upms_user_credit", "user_id", UpmsUserCreditModel.class, UpmsUserCredit.class);
		dataSource.addMapping("upms_user_login_channel", "id", UpmsUserLoginChannelModel.class, UpmsUserLoginChannel.class);
		dataSource.addMapping("upms_user_organization", "user_organization_id", UpmsUserOrganizationModel.class, UpmsUserOrganization.class);
		dataSource.addMapping("upms_user_permission", "user_permission_id", UpmsUserPermissionModel.class, UpmsUserPermission.class);
		dataSource.addMapping("upms_user_role", "user_role_id", UpmsUserRoleModel.class, UpmsUserRole.class);
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		if("server".equals(getConfig().getType())) {
			registerLocalService();
		} else if("client".equals(getConfig().getType())) {
			registerRemoteService();
		} 
	}
			
	public void registerLocalService() {
		registerLocalService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerLocalService(String group, String version, int port) {
	}
	
	public void registerRemoteService() {
		registerRemoteService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerRemoteService(String group, String version, int port) {
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	public String getRpcVersion() {
		return getConfig().getRpcVersion();
	}
	public UpmsConfig getConfig() {
		return Lambkit.config(UpmsConfig.class);
	}
}

